package com.math.aistudykbr.controller;

import com.math.aistudykbr.pojo.entity.ChoiceQuestion;
import com.math.aistudykbr.service.IQAService;
import com.math.aistudykbr.utils.Result;
import com.math.aistudykbr.utils.UserContext;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/qa")
public class QAController {

    private final IQAService qaService;

    /**
     * 生成选择题
     */
    @PostMapping("/generate")
    public Result<List<ChoiceQuestion>> generateQuestions(
            @RequestParam Long materialId,
            @RequestParam(defaultValue = "5") Integer count) {
        try {
            List<ChoiceQuestion> questions = qaService.generateQuestions(materialId, count);
            return Result.success(200, "生成题目成功", questions);
        } catch (Exception e) {
            log.error("生成题目失败", e);
            return Result.failure(500,"生成题目失败：" + e.getMessage());
        }
    }

    /**
     * 获取指定资料的题目列表
     */
    @GetMapping("/material/get")
    public Result<List<ChoiceQuestion>> getQuestionsByMaterial(@RequestParam Long materialId) {
        try {
            List<ChoiceQuestion> questions = qaService.getQuestionsByMaterialId(materialId);
            return Result.success(200, "获取题目成功", questions);
        } catch (Exception e) {
            log.error("获取题目失败", e);
            return Result.failure(500,"获取题目失败：" + e.getMessage());
        }
    }

    /**
     * 提交单题答案
     */
//    @PostMapping("/submit")
//    public Result<Boolean> submitAnswer(
//            @RequestParam Long questionId,
//            @RequestParam String answer) {
//        try {
//            Long userId = UserContext.getUser();
//            Boolean isCorrect = qaService.submitAnswer(userId, questionId, answer);
//            return Result.success(200, "提交答案成功", isCorrect);
//        } catch (Exception e) {
//            log.error("提交答案失败", e);
//            return Result.failure(500,"提交答案失败：" + e.getMessage());
//        }
//    }

//    @PostMapping("/submit-batch")
//    public Result<Map<Long, Boolean>> submitAnswers(
//            @RequestBody Map<Long, String> answers,
//            @RequestParam Long materialId) {
//        try {
//            Long userId = UserContext.getUser();
//            Map<Long, Boolean> results = qaService.submitAnswersBatch(userId, answers, materialId);
//            return Result.success(200, "批量提交答案成功", results);
//        } catch (Exception e) {
//            log.error("批量提交答案失败", e);
//            return Result.failure(500, "批量提交答案失败：" + e.getMessage());
//        }
//    }

    /**
     * 批量提交答案
     */
    @PostMapping("/submit-batch")
    public Result<Map<Long, Boolean>> submitAnswers(@RequestBody SubmitAnswersRequest request) {
        try {
            Long userId = UserContext.getUser();
            Map<Long, Boolean> results = qaService.submitAnswersBatch(
                    userId,
                    request.getAnswers(),
                    request.getMaterialId()
            );
            return Result.success(200, "批量提交答案成功", results);
        } catch (Exception e) {
            log.error("批量提交答案失败", e);
            return Result.failure(500, "批量提交答案失败：" + e.getMessage());
        }
    }
}

@Data
class SubmitAnswersRequest {
    private Map<Long, String> answers;
    private Long materialId;
}
